var readdata = function () {
    let data = $("#read-node").val()
    if (data.length < 1) return false
    data = JSON.parse(data)
    data = data.records
    console.log(data)
    let text = []
    for (i = 0; i < data.length; i++) {
        if (data[i].name) {
            text.push(data[i].name)
        }else if (data[i].server_filename) {
            text.push(data[i].server_filename)
        }
    }
    if (data.msg_list) {
        for (j = 0; j < data.msg_list.length; j++) {
            for (k = 0; k < data.msg_list[j].file_list.length; k++) {
                text.push(data.msg_list[j].file_list[k].server_filename)
                console.log(data.msg_list[j].file_list[k].server_filename);
            }
        }
    }
    $("#add-node").val(text.join("\n"))

}
//name file_list server_filename path
var addNode = function () {
    let pid = $("#pid").val()
    let data = $("#add-node").val()

    $.ajax({
        url: "./areas.php?c=bdptree&a=add",
        type: "post",
        data: {pid: pid, data: data},
        success: function (res) {
            index()
            $("#pid").val('')
            $("#add-node").val('')
            $("#read-node").val('')
            $("html,body").animate({scrollTop:$("#line-"+pid).offset().top},1000)
        }
    })
};
function index() {
    $.ajax({
        url: "./areas.php?c=bdptree&a=index",
        type: "post",
        success: function (res) {
            res = JSON.parse(res)
            let html = `<ul class="list-group">`
            html += childs(res)
            html += `</ul`
            $("#level-0").html(html)
        }
    })
};
(function () {
   index()
})()
function childs(data) {
    let html = ``
    for (let i = 0; i < data.length; i++) {
        if (typeof data[i] != 'undefined') {
            let text = "~ ".repeat(data[i].level) + data[i].name
            html += `<li id="line-${data[i].id}" class="list-group-item" onclick="setNode(${data[i].id})">${text}</li>`
            if (data[i].child && data[i].child.length > 0) {
                child = childs(data[i].child)
                html += child
            }
        }
    }
    return html
}
function childrens(data, level = '0') {
    let html = ``
    let child = ``
    for (let i = 0; i < data.length; i++) {
        if (typeof data[i] != 'undefined') {
            if (data[i].child && data[i].child.length > 0) {
                child = childrens(data[i].child, level + "-" + data[i].level)
            }
            html += `<div class="accordion-item">
                        <h2 class="accordion-header" id="head-${level}-${data[i].id}">
                            <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse-${level}-${data[i].id}" aria-expanded="false" aria-controls="collapse-${level}-${data[i].id}">
                                ${data[i].name}
                            </button>
                        </h2>
                        <div id="collapse-${level}-${data[i].id}" class="accordion-collapse collapse" aria-labelledby="head-${level}-${data[i].id}" data-bs-parent="#level-${level}">
                            <div class="accordion-body" id="level-${level}">
                                <input type="button" class="btn btn-primary" onclick="setNode(${data[i].id})" value="增加子节点">
                                ${child}
                            </div>
                        </div>
                    </div>`
        }
    }
    return html
}

var setNode = function (id) {
    $("#pid").val(id).focus()
}